package com.example.vote.controller;


import com.example.vote.entity.TpItems;
import com.example.vote.entity.TpVote;
import com.example.vote.mapper.TpVoteMapper;
import com.example.vote.service.ITpVoteService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yc.vcode.bean.Result;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 投票系统-题目表-2 前端控制器
 * </p>
 *
 * @author 廖老师
 * @since 2023-12-04
 */
@Controller
@RequestMapping("/vote/tpVote")
public class TpVoteController {

    @Resource
    TpVoteMapper voteMapper;

    @GetMapping("query")
    @ResponseBody
    public Map<String,Object> query(
            String vname,
            String sort,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size,
            @SessionAttribute(required = false) Map<String,Object> loginedUser){
        if("timeAsc".equals(sort)){
            sort = "start_date";
        } else if("timeDesc".equals(sort)){
            sort = "start_date desc";
        } else {
            sort = null;
        }
        final Page<Object> page1 = PageHelper.startPage(page, size);
        final List<TpVote> tpVotes = voteMapper.selectByVname(vname,sort);
        for (TpVote tpVote : tpVotes) {
            final Result result = getZan(tpVote.getId(), loginedUser);
            int i = result.getData() == null ? 0 : Integer.parseInt(result.getData()+"");
            tpVote.setZan(i);
        }
        System.out.println(page1.equals(tpVotes));
        Map<String,Object> res = new HashMap<>();
        res.put("records", page1);
        res.put("total", page1.getTotal());
        return res;
    }

    @GetMapping("queryById")
    @ResponseBody
    public TpVote queryById(int id){
        return voteMapper.selectById(id);
    }

    @Resource
    ITpVoteService voteService;

    @PostMapping("commit")
    @ResponseBody
    public Result commit(@RequestBody @Valid TpVote vote, Errors errors){
        System.out.println("vote.getStartDate() = " + vote.getStartDate());
        System.out.println("vote.getEndDate() = " + vote.getEndDate());
        if(vote.getStartDate() != null
            && vote.getEndDate() != null
            && vote.getStartDate().compareTo(vote.getEndDate())>0){
            errors.rejectValue("endDate","createTo",
                    "开始日期不能大于结束日期");
        }
        for (TpItems item : vote.getItems()) {
            if(item.getIname() == null || item.getIname().trim().isEmpty()){
                errors.rejectValue("items","textEmpty",
                        "所有的选项都必须要填写完整!");
                break;
            }
        }
        if(errors.hasErrors()){
            return new Result(0, "投票数据验证错误!", errors.getAllErrors());
        }
        voteService.commit(vote);
        return new Result(1,"新增投票成功!",null);
    }

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    // value 1 赞,  -1 踩,   0 取消
    @RequestMapping("setZan")
    @ResponseBody
    public Result setZan(int vid,
                       @SessionAttribute Map<String,Object> loginedUser,
                       int value){
        final BoundHashOperations<String, Object, Object> ops
                = redisTemplate.boundHashOps("zan:" + vid);
        final Object uid = loginedUser.get("id");
        ops.put(uid, value);
        return new Result(1,"操作成功!",null);
    }

    @RequestMapping("getZan")
    @ResponseBody
    public Result getZan(int vid,
                      @SessionAttribute(required = false) Map<String,Object> loginedUser){
        if(loginedUser == null){
            return new Result(0,"请先登录!", null);
        }
        final BoundHashOperations<String, Object, Object> ops
                = redisTemplate.boundHashOps("zan:" + vid);
        final Object uid = loginedUser.get("id");
        final Object value = ops.get(uid);
        return new Result(1,"操作成功!",value);
    }

}

